package gui.Server;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import system.InitiateServer;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
/**
 * this class use as the  menu for Server Running Screen
 * @author G8
 *
 */
public class ServerRunningScreen extends JPanel implements ActionListener{
	private static JTextField textFieldStatus;
	private static JTextField textFieldConUsers;
	private JButton btnDisconnect;
	
	/**
	 * Server Running Screen Constructor 
	 */
	public ServerRunningScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Server Information", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.menu);
		setBounds(InitiateServer.guiX,InitiateServer.guiY,500,400);
		setLayout(null);
		
		JLabel lblServerStatus = new JLabel("Server Status : ");
		lblServerStatus.setBounds(97, 83, 132, 14);
		add(lblServerStatus);
		
		JLabel labelConUsers = new JLabel("Connected Users : ");
		labelConUsers.setBounds(97, 105, 132, 14);
		add(labelConUsers);
		
		textFieldStatus = new JTextField();
		textFieldStatus.setEditable(false);
		textFieldStatus.setBounds(263, 80, 86, 20);
		textFieldStatus.setText(InitiateServer.serverStatus);
		add(textFieldStatus);
		textFieldStatus.setColumns(10);
		
		textFieldConUsers = new JTextField();
		textFieldConUsers.setText(Integer.toString(InitiateServer.connectedUsers));
		textFieldConUsers.setEditable(false);
		textFieldConUsers.setColumns(10);
		textFieldConUsers.setBounds(263, 102, 86, 20);
		add(textFieldConUsers);
		
		btnDisconnect = new JButton("Disconnect");
		btnDisconnect.setBounds(176, 143, 132, 38);
		btnDisconnect.addActionListener(this);
		add(btnDisconnect);
		
		JLabel lblDateTime = new JLabel("Date and Time");
		lblDateTime.setBounds(201, 332, 97, 14);
		lblDateTime.setText(InitiateServer.getDateTime());
		add(lblDateTime);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==btnDisconnect){
			ServerGUI.sv.stopListening();
			JOptionPane.showMessageDialog(null, "Server is not running !", "Success",
                    JOptionPane.INFORMATION_MESSAGE);
			ServerGUI.sv.disconnect();
			InitiateServer.serverPanel = new ServerGUI();
			InitiateServer.setScreen(this, InitiateServer.serverPanel);
		}
		
	}
	/**
	 * This method show the amount of active user in the system
	 * @param num Of Users
	 */
	public static void updateConnectedUsers(int numOfUsers){
		textFieldConUsers.setText(Integer.toString(numOfUsers));
	}
}
